package Controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"m/Global"
	"m/model"
	"net/http"
	"strconv"
)

func ShowAmount(context *gin.Context)  {
	fmt.Println("进入showamount")
	var amount []model.Account
	err := Global.Db.Find(&amount).Error
	if err != nil {
		fmt.Println("查询全体账目出错！")
		return
	}
	fmt.Println("数据库查到的全体账目信息：", amount)
	context.JSON(http.StatusOK, amount)
}

func CountAmount(context *gin.Context)  {
	// 调用存储过程
	var count int
	Global.Db.Raw("select count_account()").First(&count)
	fmt.Println("账目数量：", count)
	context.JSON(200, gin.H{
		"code":200,
		"msg":"返回账目数量成功",
		"data":count,
	})
}

func Pay(context *gin.Context) {
	fmt.Println("进入Pay")
	var requestAccount = model.Account{}
	json.NewDecoder(context.Request.Body).Decode(&requestAccount)
	fmt.Println("即将更新学号：", requestAccount.Sno, "课号：", requestAccount.Cno, "金额：", requestAccount.Aamount)

	if len(requestAccount.Sno) < 1 {
		fmt.Println("学号不能为空！", len(requestAccount.Sno))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"学号不能为空",
		})
		return
	}
	if len(requestAccount.Cno) < 1 {
		fmt.Println("课号不能为空！", len(requestAccount.Cno))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"课号不能为空",
		})
		return
	}
	if len(requestAccount.Aamount) < 1 {
		fmt.Println("金额不能为空！", len(requestAccount.Aamount))
		context.JSON(200, gin.H{
			"code":200,
			"msg":"金额不能为空",
		})
		return
	}

	//给自己缴费
	fmt.Println("Global.Sno = ", Global.Sno, "Req.Sno = ", requestAccount.Sno)
	if Global.Sno != requestAccount.Sno {
		fmt.Println("只能给自己缴费！")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"只能给自己缴费！",
		})
		return
	}

	// 学号是否存在
	var student model.Student
	Global.Db.Where("sno = ?", requestAccount.Sno).First(&student)
	fmt.Println("student.Sno=", student.Sno)
	if student.Sno == "" {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"找不到此学生",
		})
		return
	}
	// 课号是否存在
	var course model.Course
	Global.Db.Where("cno = ?", requestAccount.Cno).First(&course)
	fmt.Println("course.Cno=", course.Cno)
	if course.Cno == "" {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"找不到此课程",
		})
		return
	}
	//是否选了这门课
	var keystudent model.Keystudent
	erre := Global.Db.Where("sno = ? and cno = ?", requestAccount.Sno, requestAccount.Cno).First(&keystudent).Error
	fmt.Println("keystudents.Sno = ", keystudent.Sno)
	if erre != nil {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"您还没选择此门课程！",
		})
		return
	}

	// 查询课程费用
	var tempCost model.Course
	var cost string
	Global.Db.Select("camount").Where("cno = ?", requestAccount.Cno).First(&tempCost)
	cost = tempCost.Camount
	fmt.Println("课程费用为：", cost)
	money, err := strconv.Atoi(cost)
	if err != nil {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"类型转换出错1",
		})
		return
	}
	pay, err := strconv.Atoi(requestAccount.Aamount)
	if err != nil {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"类型转换出错2",
		})
		return
	}

	// 缴费金额是否正确
	if requestAccount.Aamount == "" || money != pay{
		context.JSON(200, gin.H{
			"code":200,
			"msg":"请参考课程费用并输入正确的金额",
		})
		return
	}

	// 是否已经交过费
	var coin model.Account
	Global.Db.Select("aamount").Where("sno = ? and cno = ?", requestAccount.Sno, requestAccount.Cno).First(&coin)
	if requestAccount.Aamount == coin.Aamount {
		context.JSON(200, gin.H{
			"code":200,
			"msg":"已经交过费，无需再缴费",
		})
		return
	}

	// 把这个学生的累计交款额写入students表
	var tempStudent model.Student
	Global.Db.Where("sno = ?", requestAccount.Sno).First(&tempStudent)
	fmt.Println("tempStudent.Sno = ", tempStudent.Sno)
	if tempStudent.Samount == "" {
		tempStudent.Samount = "0"//第一次缴费，设置为0
	}
	fmt.Println("转换前：", tempStudent.Samount)
	tempCount, err := strconv.Atoi(tempStudent.Samount)
	fmt.Println("转换后：", tempCount)
	if err != nil {
		fmt.Println("类型转换出错3")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"类型转换出错3",
		})
		return
	}
	fmt.Println("emmmm", requestAccount.Aamount)
	tempCoin, err := strconv.Atoi(requestAccount.Aamount)
	if err != nil {
		fmt.Println("类型转换出错4")
		context.JSON(200, gin.H{
			"code":200,
			"msg":"类型转换出错4",
		})
		return
	}
	tempCount += tempCoin //此学生的累计缴费金额
	tempStudent.Samount = strconv.Itoa(tempCount)
	Global.Db.Where("sno = ?", requestAccount.Sno).Updates(&tempStudent)


	var tempAccount model.Account
	Global.Db.Where("sno = ?", requestAccount.Sno).Where("cno = ?", requestAccount.Cno).First(&tempAccount)
	// 把缴费金额写入数据库
	correct := model.Account{
		Sno: requestAccount.Sno,
		Cno: requestAccount.Cno,
		Adate: tempAccount.Adate,
		Aclass: tempAccount.Aclass,
		Aamount: strconv.Itoa(pay),
	}
	Global.Db.Where("sno = ?", requestAccount.Sno).Where("cno = ?", requestAccount.Cno).Updates(&correct)

	context.JSON(http.StatusOK, gin.H{
		"code":200,
		"msg":"支付成功",
	})
}
